﻿/*
	zealotwms 仓库管理系统
	Copyright (C) 2011  狂热者工作组 
    内容：LoginForm 登录窗体
    时间: 2011-9-5
*/
using System;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Client.Helper;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client
{
    public partial class LoginForm : BaseForm
    {
        #region 构造函数
        public LoginForm()
        {
            InitializeComponent();
        }
        #endregion

        #region 控件函数

        private void Control_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender);
        }

        private void Control_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender);
        }

        private void tb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Login(tbUserCode.Text, tbPassword.Text);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login(tbUserCode.Text,tbPassword.Text);
        }

        //private void btnChangePwd_Click(object sender, EventArgs e)
        //{

        //}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion

        #region 私有函数
        private void Login(string sUsercode,string sPassworbd)
        {
            //SplashScreen.BeginDisplay();
            //SplashScreen.SetCommentaryString("正在登陆系统，请稍候...");
            this.Visible = false;

            if (sUsercode.Trim() == "")
            {
                this.Visible = true;
                MessageBox.Show("用户名不能为空!");
                return;
            }

           ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            SysAccount account = client.GetAccountByUserCode(sUsercode);
            
            if (account == null)
            {
                SplashScreen.EndDisplay();
                this.Visible = true;
                MessageBox.Show("该用户不存在!");
                return;
            }

            if (account.IsLocked == "0")
            {
                MessageBox.Show("该用户被锁定!");
                return;
            }

            if (GetHashValue.Cryptography.GetHashValue(sPassworbd) != account.Password.Trim())
            {
                SplashScreen.EndDisplay();
                this.Visible = true;
                MessageBox.Show("密码错误!");
                return;
            }

            DateTime dtNow = client.GetCurrentSystemTime();
            MainForm.CurrentUser = account;
            account.LastLoginDate = CommonHelper.GetDateString(dtNow);
            account.LastLoginTime = CommonHelper.GetTimeString(dtNow);
            client.UpdateAccount(account);
            InsertLogInf(account);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void InsertLogInf(SysAccount record)
        {
            try
            {
                SysLoginInf logRecord = new SysLoginInf();
                logRecord.UserCode = record.Id;
                logRecord.UserName = record.UserName;
                logRecord.DepartmentCode = record.DepartmentCode;
                logRecord.DepartmentName = record.DepartmentName;
                logRecord.SysDate = record.LastLoginDate;
                logRecord.LogTime = record.LastLoginTime;
                logRecord.LogSgn = "0";
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                client.AddLogInf(logRecord);
            }
            catch
            {
                MessageBox.Show("写入登陆日志失败！");
                return;
            }
            
 
        }

        #endregion
    }
}
